<?php


namespace app\backstage\controller;


use app\backstage\model\RankAgent;
use app\backstage\model\RankChannel;
use app\backstage\model\RankEb;
use app\backstage\model\RankQa;
use app\common\command\RankReport;
use think\Controller;
use think\Db;

class Rank extends Controller
{

    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    public function channel(){
        if (request()->isAjax()) {
            $page = input('page', 1);
            $limit = input('limit', 10);
            $channel_id = input('channel_id');
            $field = input('field','channel_id');
            $order = input('order','desc');

            $orderBy = "{$field} {$order}";
            $where = [];
            if ($channel_id != '') {
                $where[] = ['channel_id', '=', $channel_id];
            }
            $list = RankChannel::with(['channel'])->where($where)->order($orderBy)->page($page,$limit)->select();
            $count = RankChannel::where($where)->count();
            return sucJson($list, $count);
        }
        return $this->fetch();
    }

    public function agent(){
        if (request()->isAjax()) {
            $page = input('page', 1);
            $limit = input('limit', 10);
            $agent_id = input('agent_id');
            $field = input('field','agent_id');
            $order = input('order','desc');

            $orderBy = "{$field} {$order}";
            $where = [];
            if ($agent_id != '') {
                $where[] = ['agent_id', '=', $agent_id];
            }
            $list = RankAgent::with(['agent'])->where($where)->order($orderBy)->page($page,$limit)->select();
            $count = RankAgent::where($where)->count();
            return sucJson($list, $count);
        }
        return $this->fetch();
    }


    public function qa(){
        $dates = array();
        for ($i = 7 - 1; $i >= 0; $i--) {
            $dates[] = date('Y-m-d', strtotime("-$i days"));
        }
        if (request()->isAjax()) {
            $page = input('page', 1);
            $limit = input('limit', 10);
            $quick_id = input('quick_id');
            $field = input('field','quick_id');
            $order = input('order','desc');

            $orderBy = "{$field} {$order}";
            $where = [];
            if ($quick_id != '') {
                $where[] = ['quick_id', '=', $quick_id];
            }
            $list = RankQa::with(['qa'])->where($where)->order($orderBy)->page($page,$limit)->select();
            $count = RankQa::where($where)->count();
            return sucJson($list, $count);
        }
        return $this->fetch();
    }

    /**
     * 刷新数据
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function refreshData()
    {
        $type = input('type');
        $obj = new RankReport();
        if ($type == 'agent') {
            $obj->agent();
            return sucJson();
        } elseif ($type == 'channel') {
            $obj->channel();
            return sucJson();
        } elseif ($type == 'qa') {
            $obj->qa();
            return sucJson();
        }  elseif ($type == 'eb') {
            $obj->eb();
            return sucJson();
        }else {
            return errJson('类型异常');
        }
    }

    public function eb(){
        if (request()->isAjax()) {
            $page = input('page', 1);
            $limit = input('limit', 10);
            $eb_id = input('eb_id');
            $field = input('field','eb_id');
            $order = input('order','desc');

            $orderBy = "{$field} {$order}";
            $where = [];
            if ($eb_id != '') {
                $where[] = ['eb_id', '=', $eb_id];
            }
            $list = RankEb::with(['eb'])->where($where)->order($orderBy)->page($page,$limit)->select();
            $count = RankEb::where($where)->count();
            return sucJson($list, $count);
        }
        return $this->fetch();
    }
}